package com.elite.groovy.callback

import cn.com.kh.HttpClientProxy
import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class ThirdPartyPolicyStatusNotify implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(ThirdPartyPolicyStatusNotify.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		log.debug("----------------第三方名单投保单状态回写汽车之家----------------");
		def sumnum=0;
		def returnBody="";
		//def selectSQL = "select t.qt_id  qt_id, t.status status,  t.con    con,  t.uuid   uuid, t.time1  time1 from (select w2.proposal_no qt_id, ta.insurance_status status,  count(r.customer_guid) con, lower(substr(sys_guid(), 1, 8) || '-' || substr(sys_guid(), 9, 4) || '-' ||substr(sys_guid(), 13, 4) || '-' ||  substr(sys_guid(), 17, 4) || '-' ||substr(sys_guid(), 21, 12)) uuid, to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time1 from tabccarinsurance ta left join wd_inflowtask w1 on ta.customer_guid = w1.customer_guid left join wd_quotation w2 on w1.task_guid = w2.task_guid left join record r on ta.customer_guid = r.customer_guid where ta.checkdate >= sysdate - 1 and ta.insurance_status in ('1', '3') and ta.riskcode in  (select risk_code from c_riskcode where risk_type = 1)   and ta.datatype = 'QCZJ'  group by w2.proposal_no, ta.insurance_status union all  select w2.proposal_no qt_id,  ta.insurance_status status, count(r.customer_guid) con,  lower(substr(sys_guid(), 1, 8) || '-' ||substr(sys_guid(), 9, 4) || '-' ||substr(sys_guid(), 13, 4) || '-' || substr(sys_guid(), 17, 4) || '-' || substr(sys_guid(), 21, 12)) uuid,  to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time1 from tabccarinsurance ta  left join wd_inflowtask w1 on ta.customer_guid = w1.customer_guid  left join wd_quotation w2 on w1.task_guid = w2.task_guid left join record r on ta.customer_guid = r.customer_guid where  ta.paydate >= sysdate - 1 and ta.insurance_status = '6' and ta.riskcode in (select risk_code from c_riskcode where risk_type = 1)  and ta.datatype = 'QCZJ'group by w2.proposal_no, ta.insurance_status) t";		
		def selectSQL = "select t.qt_id1 qtid1, t.qt_id2 qtid2, t.con con, t.uuid uuid, t.time1 time1,t.citycode citycode   from (select w2.proposal_no qt_id1,  ta.qt_id qt_id2,  count(r.customer_guid) con,  lower(substr(sys_guid(), 1, 8) || '-' || substr(sys_guid(), 9, 4) || '-' ||  substr(sys_guid(), 13, 4) || '-' || substr(sys_guid(), 17, 4) || '-' ||  substr(sys_guid(), 21, 12)) uuid,   to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time1 ,w3.sales_citycode citycode   from tabccarinsurance ta   left join wd_inflowtask w1  on ta.customer_guid = w1.customer_guid  and w1.last_flag = 1  left join wd_quotation w2   on w1.task_guid = w2.task_guid left join wd_sales w3 on w1.task_guid=w3.task_guid   left join record r  on ta.customer_guid = r.customer_guid where 1 = 1   and ta.paydate >= sysdate - 1  and ta.insurance_status = '6'   and ta.riskcode in  (select risk_code from c_riskcode where risk_type = 1)  and ta.datatype = 'QCZJ' group by w2.proposal_no, ta.qt_id,w3.sales_citycode) t";
		def resultList = eliteBean.chaxun_sql(selectSQL,"dataSource");
		def resultListNum = resultList.size();
		log.debug("本次调用数量H5:"+resultListNum);
						 
		if(resultListNum>0){
			def selectSQL2="SELECT WEBSERVICEURL FROM SYSC_INTERFACE where  WEBTYPE='QCZJ'";
			def postURL2=eliteBean.chaxun_sql(selectSQL2,"dataSource");
			def postURL=postURL2[0].get("WEBSERVICEURL");
			for (int i=0;i<resultListNum;i++) {	
			    def queryMap1=resultList[i];
			    def QT_ID_OLD=queryMap1.get("qtid1");
				def QT_ID_NEW=queryMap1.get("qtid2");
				def CON=queryMap1.get("con");
				def UUID=queryMap1.get("uuid");
				def TIME1=queryMap1.get("time1");
				def CITYCODE=queryMap1.get("citycode");
				def selectSQL3="select count(*) NUM from Insuredinfoback where qt_id='" + QT_ID_NEW + "' and con='" + CON + "'";
				def re_sult=eliteBean.chaxun_sql(selectSQL3,'dataSource');
				def re_num=re_sult[0].get("NUM");
				log.debug("系统调用H5发送re_num:" + re_num);
				if(re_num==0){
				String sendBody="";
				String result="";
				try {
					sendBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
						"<request>\n" +
						"<head>\n" +
						"   <msgId>" + UUID + "</msgId>\n" +
                        "   <timeStamp>" + TIME1 + "</timeStamp>\n" +
						"</head>\n" +
						" <body>\n " +
						"  <order> \n" +
						"   <orderNo>" + QT_ID_OLD + "</orderNo>\n" +
						"   <newOrderNo>" + QT_ID_NEW + "</newOrderNo>\n" +
						"   <sendType>policy</sendType>\n" +
						"   <contactCount>" + CON + "</contactCount>\n" +
						"   <groupNo>" + CITYCODE + "</groupNo>\n" +
						"  </order> \n" +
						" </body>\n" +
						"</request>";   
					log.debug("系统调用H5发送URL:" + postURL + " 发送报文:" + sendBody);
					result = HttpClientProxy.sendPost(postURL, sendBody, "", 30000);
					log.debug("系统调用H5返回结果:" + result);
				    def insertSQL2="insert into qt_dm_ifc_log_new(id,offer_no,ifc_no,send_info,return_info,send_time,return_time) values (Sys_Guid(),'" + QT_ID_NEW + "','QCZJ','" + sendBody + "','" + result + "',sysdate,sysdate)";
				    eliteBean.update(insertSQL2,'dataSource');
				    def insertSQL3="insert into INSUREDINFOBACK(qt_id,status,con,uuid,time1) values('" + QT_ID_NEW + "','6','" + CON + "','" + UUID + "',sysdate)";
				    eliteBean.update(insertSQL3,'dataSource');
                    sumnum=sumnum+1;					

				} catch (Exception e) {
					log.debug("系统调用H5异常:" + e.getMessage());
				}
				}	
			  }
			 }
			returnBody ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
						" <BODY>\n " +
						"   <count1>" + resultListNum + "</count1>\n" +
						"   <count2>" + sumnum + "</count2>\n" +
						" </BODY>";  
			//return returnBody;
			}
}